package cn.com.xxlibrary.interceptor;

import java.util.Map;
import java.util.Set;

import cn.com.xxlibrary.util.UtilTools;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.util.TextParseUtil;

/**
 * 用户登录状态验证拦截器
 * 
 * @author 学刚
 * 
 */
public class LoginInterceptor implements Interceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String loginUser;//session 中放的值；  
    private String exclude;//不需要拦截的页面;  
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void init() {
		// TODO Auto-generated method stub

	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		
		//获取当前访问的action名字
		String actionName = invocation.getProxy().getActionName();
		Set<String> set = TextParseUtil.commaDelimitedStringToSet(exclude); 
		if(set.contains(actionName)){
            return invocation.invoke();  
        }else{
        	// 获取Session
        	Map<String, Object> session = invocation.getInvocationContext().getSession();
        	Object loginManager = session.get(loginUser);
        	// 判断Session中是否有LoginedUserName
        	if (UtilTools.isNotNullorEmpty(loginManager)) {
        		// 已登陆，进行下一步操作
        		return invocation.invoke();
        	}else{
        		return invocation.invoke();
        		//return "login";
        	}
        }
	}

	public String getLoginUser() {
		return loginUser;
	}

	public void setLoginUser(String loginUser) {
		this.loginUser = loginUser;
	}

	public String getExclude() {
		return exclude;
	}

	public void setExclude(String exclude) {
		this.exclude = exclude;
	}

}
